package com.ruoyi.group.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.group.domain.Customer;
import com.ruoyi.group.service.ICustomerService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.group.mapper.CustomerVisitMapper;
import com.ruoyi.group.domain.CustomerVisit;
import com.ruoyi.group.service.ICustomerVisitService;

/**
 * 拜访信息Service业务层处理
 *
 * @author alun
 * @date 2022-11-28
 */
@Service
public class CustomerVisitServiceImpl extends ServiceImpl<CustomerVisitMapper, CustomerVisit> implements ICustomerVisitService {
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private ISysUserService userService;
    /**
     * 查询拜访信息列表
     *
     * @param customerVisit 拜访信息
     * @return 拜访信息
     */
    @Override
    public List<CustomerVisit> selectCustomerVisitList(CustomerVisit customerVisit) {

        List<CustomerVisit> customerVisits = getBaseMapper().selectCustomerVisitList(customerVisit);
        for (CustomerVisit visit : customerVisits) {
            SysUser sysUser = userService.selectUserById(visit.getInputUser());
            if (sysUser == null){
                visit.setInputUserName("管理员");
                visit.setInputTime(new Date());
            }else {
                visit.setInputUserName(sysUser.getNickName());
                visit.setInputTime(new Date());
            }
        }
        return customerVisits;

    }

    @Override
    public boolean save(CustomerVisit entity) {
        entity.setInputUser(SecurityUtils.getUserId());
        entity.setInputTime(new Date());
        return super.save(entity);
    }
    @Override
    public boolean updateById(CustomerVisit entity) {
        entity.setInputUser(SecurityUtils.getUserId());
        entity.setInputTime(new Date());

        return super.updateById(entity);
    }

}
